package cn.kgc.springcloud.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: mosin
 * @version: v1.0  2022/6/8
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private DiscoveryClient discoveryClient;  //服务发现对象
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private  RestTemplate restTemplate;
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);


    @ResponseBody
    @RequestMapping("user1")
    public String test01() {
        return "用户服务被调用";
    }


    @ResponseBody
    @RequestMapping("user2")
    public String test02() {

        //用户服务 调用部门服务  发送http请求   RestTemplate  http协议
        RestTemplate restTemplate = new RestTemplate();
        //发送get请求  请求部门服务   参数1:请求服务的接口  参数2:响应数据的类型
        String result = restTemplate.getForObject("http://" + getRandomHost() + "/dept/dept1", String.class);
        logger.info("部门服务被调用,返回的结果是 :{}", result);
        return "用户服务被调用";
    }

    /**
     * localhost:8503  localhost:8506  localhost:8507    轮循
     * localhost:8503  localhost:8506     随机
     */

    public String getRandomHost() {

        ArrayList<String> hosts = new ArrayList<String>();
        hosts.add("localhost:8503");
        hosts.add("localhost:8506");

        Random random = new Random();
        return hosts.get(random.nextInt(hosts.size()));

    }


    @ResponseBody
    @RequestMapping("user3")
    public String test03() {  // 实现服务健康检查

        //使用 restTemplate + ribbon 实现负载均衡
        RestTemplate restTemplate = new RestTemplate();
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("DEPTS");

        //serviceInstances.forEach(serviceInstance->{
        //    URI uri = serviceInstance.getUri();
        //    logger.info("服务的uri{}",uri);
        //});

        Random random = new Random();
        //发送get请求  请求部门服务   参数1:请求服务的接口  参数2:响应数据的类型
        String result = restTemplate.getForObject(serviceInstances.get(random.nextInt(serviceInstances.size())).getUri() + "/dept/dept1", String.class);
        logger.info("部门服务被调用,返回的结果是 :{}", result);
        return "用户服务被调用";
    }

    @ResponseBody
    @RequestMapping("user4")
    public String test04() {  // 实现服务健康检查

        //使用 restTemplate + ribbon 实现负载均衡
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance  serviceInstance = loadBalancerClient.choose("DEPTS");
        //发送get请求  请求部门服务   参数1:请求服务的接口  参数2:响应数据的类型
        String result = restTemplate.getForObject( serviceInstance.getUri()+"/dept/dept1", String.class);
        logger.info("部门服务被调用,返回的结果是 :{}", result);
        return "用户服务被调用";
    }


    @ResponseBody
    @RequestMapping("user5")
    public String test05() {  // 实现服务健康检查
        //通过服务名 实现服务之间的调用
        String result = restTemplate.getForObject( "http://DEPTS/dept/dept1", String.class);
        logger.info("部门服务被调用,返回的结果是 :{}", result);
        return "用户服务被调用";
    }

}